package top.datacluster.basic.luence;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;


import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;


public class LuenceTest {

    public static void main(String[] args) throws IOException {
        Path path = FileSystems.getDefault().getPath("/Users/sai/IdeaProject/basic_algorithm/data/index");
        try {
            //1:指定索引库的地址
            FSDirectory directory = FSDirectory.open(path);
            //2 创建分词器，标准分词器
            StandardAnalyzer analyzer = new StandardAnalyzer();
            //3:创建IndexWriterConfig
            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            //4:创建IndexWriter
            IndexWriter writer = new IndexWriter(directory, config);
            writer.deleteAll();//清除以前的index
            File file = new File("/Users/sai/IdeaProject/basic_algorithm/data/file");

            File[] listFiles = file.listFiles();

            for (File f : listFiles) {
                Document document = new Document();
                document.add(new TextField("content",new FileReader(f)));
                document.add(new TextField("name", f.getName(), Field.Store.YES));
                document.add(new TextField("path",f.getAbsolutePath(),Field.Store.YES));

                writer.addDocument(document);
            }

            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
